package com.grao.waterschool.human.controller;
//关于登陆
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.util.SaResult;

import com.grao.waterschool.human.common.R;
import com.grao.waterschool.human.entiry.dto.CreateUserDTO;
import com.grao.waterschool.human.entiry.dto.LoginInfo;
import com.grao.waterschool.human.entiry.dto.UserInfoDTO;
import com.grao.waterschool.human.entiry.dto.UserLogin;
import com.grao.waterschool.human.entiry.po.TWschoolRole;
import com.grao.waterschool.human.entiry.vo.ResetPassword;
import com.grao.waterschool.human.manager.service.LoginService;
import com.grao.waterschool.human.manager.service.RoleService;
import com.grao.waterschool.human.manager.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Login")
@Tag(name = "用户登陆相关接口")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    @SaIgnore
    @Operation(summary = "登陆（Oauth）接口")
    @Parameter(name = "UserLogin", description = "用户登录参数")
    @PostMapping("/loginForAuth")
    public R<Object> login(@RequestBody LoginInfo info) throws Exception {
        SaResult token = loginService.loginForOAuth2(info);
        if (token.getCode() != 200) {
            return R.error(token.getMsg());
        }
        return R.success(token.getData());
    }

    @SaIgnore
    @Operation(summary = "登陆接口")
    @Parameter(name = "UserLogin", description = "用户登录参数")
    @PostMapping("/login")
    public R<Object> login(@RequestBody UserLogin userLogin) throws Exception {
        SaResult token = loginService.login(userLogin);
        if (token.getCode() != 200) {
            return R.error(token.getMsg());
        }
        return R.success(token.getData());
    }

    @SaIgnore
    @Operation(summary = "创建用户接口")
    @Parameter(name = "CreateUserDTO", description = "创建用户参数")
    @PostMapping("/creatuser")
    public R<SaResult> creatUser(@RequestBody CreateUserDTO tuser) {
        SaResult user = userService.createUser(tuser);
        if (user.getCode() == 400) {
            return R.error("用户已存在");
        }
        return R.success(user.getMsg());
    }

    @SaIgnore
    @Operation(summary = "退出登陆接口")
    @GetMapping("/logout")
    public R<Object> logout() {
        SaResult logout = loginService.logout();
        if (logout.getCode() != 200) {
            return R.error(logout.getMsg());
        }
        return R.success(logout.getData());
    }

    @SaCheckLogin
    @Operation(summary = "获取用户信息接口")
    @GetMapping("/getUserInfo")
    public R<Object> getUserInfo() {
        SaResult userInfo = loginService.getUserInfo();
        if (userInfo.getCode() != 200) {
            return R.error(userInfo.getMsg());
        }
        return R.success(userInfo.getData());
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "查看全部用户列表")
    @GetMapping("/getUserList")
    public R<List<UserInfoDTO>> getUserList() {
        List<UserInfoDTO> saResult = userService.SelectUserList();
        return R.success(saResult);
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "权限列表")
    @GetMapping("/getPermissions")
    public R<Object> getPermissions() {
        List<TWschoolRole> permissionList = roleService.getRoleList();
        return R.success(permissionList);
    }

    @Operation(summary = "重置密码")
    @Schema(description = "ResetPassword:String teacherNum;String newPassword")
    @PostMapping("/resetPassword")
    public R<Object> resetPassword(@RequestBody ResetPassword resetPassword) {
        SaResult result = loginService.resetPassword(resetPassword);
        return result.getCode() == 200 ? R.success(result.getMsg()) : R.error(result.getMsg());
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "禁用/启用用户")
    @PostMapping("/disableUser")
    public R<Object> disableUser(@RequestParam("teacherNum") String teacherNum) {
        SaResult result = loginService.disableUserByTeacherId(teacherNum);
        return result.getCode() == 200 ? R.success(result.getMsg()) : R.error(result.getMsg());
    }

}
